package com.money.controller;

import com.github.pagehelper.PageInfo;
import com.money.entity.Book;
import com.money.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class BookController {
    @Resource
    private BookService bookService;

    @RequestMapping("/selectBooks")
    public String selectBooks(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                              @RequestParam(required = false,defaultValue = "5") Integer size,
                              @ModelAttribute("author") String author,
                              @ModelAttribute("publish") String publish, Model model
                              ) {
        PageInfo<Book> bookPageInfo = bookService.bookList(pageNo, size, author, publish);
        model.addAttribute("bookPageInfo",bookPageInfo);
        return "bookList";
    }
    //退出方法
    @RequestMapping("/exit")
    public String exit(HttpSession session){
        session.invalidate();
        return "login";
    }
    //添加图书
    @RequestMapping("/insertBook")
    public String addBooks(Book book){
        bookService.addBooks(book);
        return "addBooksOk";
    }
    //查询一个图书
    @RequestMapping("/selectOneBook")
    public String selectOneBook(Integer bookId,Model model){
        Book book = this.bookService.selectOne(bookId);
        model.addAttribute("book",book);
        return "viewBook";
    }
    @RequestMapping("/updateBook")
    public String updateBook(Book book){
        this.bookService.updateBook(book);
        return "updateOk";
    }
    //删除图书
    @RequestMapping("/deleteBook")
    public String deleteBook(Integer bookId){
        this.bookService.deleteUser(bookId);
        return "deleteOk";
    }
}
